//
//  HotWindowCollectionViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/1.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "HotWindowCollectionViewCell.h"
#import "GXMacro.h"
#import "cellOfPicCollectionView.h"
#import "ModelOfCellPic.h"
@interface HotWindowCollectionViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UIView *viewOfCollectionView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout * layout;
@end
@implementation HotWindowCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _labelOfTitle = [[UILabel alloc] init];
        [self.contentView addSubview:_labelOfTitle];
        _numberOfCut = [[UILabel alloc] init];
        [self.contentView addSubview:_numberOfCut];
        _viewOfCollectionView = [[UIView alloc] init];
        [self.contentView addSubview:_viewOfCollectionView];
        [_labelOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).mas_equalTo(10);
            make.right.mas_equalTo(self.contentView.mas_right).mas_equalTo(-10);
            make.top.mas_equalTo(self.contentView.mas_top);            make.height.mas_equalTo(20);
        }];
        [_numberOfCut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).mas_equalTo(10);
            make.top.mas_equalTo(_labelOfTitle.mas_bottom);
            make.right.mas_equalTo(self.contentView.mas_right).mas_equalTo(-10);
            make.height.mas_equalTo(20);
        }];
        [_viewOfCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).mas_equalTo(10);
            make.right.mas_equalTo(self.contentView.mas_right).mas_equalTo(-10);
            make.top.mas_equalTo(_numberOfCut.mas_bottom);
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
        }];
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        [_viewOfCollectionView addSubview:_collectionView];
        _viewOfCollectionView.layer.masksToBounds = YES;
        _viewOfCollectionView.layer.borderWidth = 1;
        _viewOfCollectionView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).mas_equalTo(10);
            make.right.mas_equalTo(self.contentView.mas_right).mas_equalTo(-10);
            make.top.mas_equalTo(_numberOfCut.mas_bottom);
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
        }];
        self.layout.minimumLineSpacing = 0;
        self.layout.minimumInteritemSpacing = 0;
        self.layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[cellOfPicCollectionView class] forCellWithReuseIdentifier:@"cellOfPicCollectionView"];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self layoutIfNeeded];
    

    self.layout.itemSize = CGSizeMake((self.contentView.bounds.size.width - 35) / 2, (self.contentView.bounds.size.height - 55) / 2);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfPic.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    cellOfPicCollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellOfPicCollectionView" forIndexPath:indexPath];
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dic in _arrayOfPic) {
        ModelOfCellPic *model = [[ModelOfCellPic alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [array addObject:model];
    }
    ModelOfCellPic *model = [array objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.commend_image]];
    return cell;
}
- (void)setArrayOfPic:(NSArray *)arrayOfPic
{
    _arrayOfPic = arrayOfPic;

    [_collectionView reloadData];
}
- (void)setIndex:(NSIndexPath *)index
{
    _index = index;
    [_collectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(whenYouPressMeWithIndex:)]) {
        [self.delegate whenYouPressMeWithIndex:_index];
    }
}

@end
